<?php
Configure::write('debug', 0);
$mmlText = array();
$track = 1;
$i = 0;
foreach ($mmls as $mml){
	$inst = $mml['Mml']['instrument_id']-1;
	$pan = $mml['Mml']['pan'];	// パンポット
	if ($pan == '128') $pan = 127;
	$inst_name = $mml['Instrument']['name'];	// 楽器名（デバッグ用）
	$track_name = $mml['Mml']['name'];

	if ($track_name == '') {
		$track_name = $inst_name;
	}else{
		$track_name = $track_name;
	}
	if ($isDram != 0) $inst = $inst_isdram;
	// MMLをパース。（楽譜ごとの処理）
	if ($mml['Mml']['ch1']){
		if ($track == 10) $track++;	// 10トラック目は使用不可能
		$mmlText[] = '[ChannelProperty'.$track.']';
		$mmlText[] = 'Name='.$track_name.' - '.__('Melody',true);
		$mmlText[] = 'Patch='.$inst;
		$mmlText[] = 'Volume=100';
		$mmlText[] = 'Panpot='.$pan;
		$mmlText[] = 'Marker=0';
		$mmlText[] = '';
		$mmlText[] = '[channel'.$track.']';
		$mmlText[] = chunk_split($mml['Mml']['ch1'],70,"\n");
		$track++;
	}
	if ($mml['Mml']['ch2']){
		if ($track == 10) $track++;
		$mmlText[] = '[ChannelProperty'.$track.']';
		$mmlText[] = 'Name='.$track_name.' - '.__('Chord 1',true);
		$mmlText[] = 'Patch='.$inst;
		$mmlText[] = 'Volume=100';
		$mmlText[] = 'Panpot='.$pan;
		$mmlText[] = 'Marker=0';
		$mmlText[] = '';
		$mmlText[] = '[channel'.$track.']';
		$mmlText[] = chunk_split($mml['Mml']['ch2'],70,"\n");
		$track++;
	}
	if ($mml['Mml']['ch3']){
		if ($track == 10) $track++;
		$mmlText[] = '[ChannelProperty'.$track.']';
		$mmlText[] = 'Name='.$track_name.' - '.__('Chord 2',true);
		$mmlText[] = 'Patch='.$inst;
		$mmlText[] = 'Volume=100';
		$mmlText[] = 'Panpot='.$pan;
		$mmlText[] = 'Marker=0';
		$mmlText[] = '';
		$mmlText[] = '[channel'.$track.']';
		$mmlText[] = chunk_split($mml['Mml']['ch3'],70,"\n");
		$track++;
	}
}
header('Content-Type: text/mml');
header('Content-Disposition: attachment; filename="'.$mmls['0']['Article']['title'].'.mle.mml"');
?>
[Settings]
Encoding=UTF-8
title=<?php echo $mmls['0']['Article']['title']."\n"; ?>
Source=<?php echo $mmls['0']['Article']['source']."\n"; ?>
memo=MML Created by <?php echo $mmls['0']['User']['username']; ?> | http://mabinogi.logue.be/articles/<?php echo $mmls['0']['Article']['id']."\n"; ?>
TimeBase=96
TimeSignatureNN=4
TimeSignatureDD=4
Genelater=Lorelei<http://mabinogi.logue.be/> 3MLE Exporter v1.2

<?php echo join("\n",$mmlText);	// マージ ?>